﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using W.Common.Enum;
using W.Warehouse.Api.Application.Command.StorageCommand;
using W.Warehouse.Domain;
using W.Warehouse.Repository.Interface;

namespace W.Warehouse.Api.Application.CommandHandlder.StorageCommandHandlder
{
    public class StorageUpdateCommandHandlder : IRequestHandler<StorageUpdateCommand, ApiResult<int>>
    {
        private readonly ILogger<StorageUpdateCommandHandlder> _logger;
        private readonly IStorageRepository _storageRepository;
        private readonly IStorageProductsRepository _storageProductsRepository;
        public StorageUpdateCommandHandlder(ILogger<StorageUpdateCommandHandlder> logger, IStorageRepository storageRepository, IStorageProductsRepository storageProductsRepository)
        {
            _logger = logger;
            _storageRepository = storageRepository;
            _storageProductsRepository = storageProductsRepository;
        }
        public async Task<ApiResult<int>> Handle(StorageUpdateCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            _logger.LogInformation("修改入库成功");
            //System.Data.IsolationLevel.Serializable
            using (var tran = _storageRepository.DB.Database.BeginTransaction(System.Data.IsolationLevel.Serializable))
            {
                try
                {
                    //入库表修改一条数据
                    var now = DateTime.Now;
                    Storage storage = new Storage()
                    {
                        StorageId = request.StorageId,
                        StorageNo = request.StorageNo,
                        StorageType = request.StorageType,
                        AssociateNumber = request.AssociateNumber,
                        VendorNo = request.VendorNo,
                        VendorName = request.VendorName,
                        VendorContact = request.VendorContact,
                        VendorPhone = request.VendorPhone,
                        SingleMaker = request.SingleMaker,
                        SingleTime = request.SingleTime,
                        StorageRemarks = request.StorageRemarks,
                        StorageStatus = AuditStatus.等待审核,
                        ExamineReason = request.ExamineReason,
                        ExaminePeople = request.ExaminePeople,
                        ExamineTime = request.ExamineTime,
                        CreateTime = request.CreateTime,
                        UpdateTime = now,
                        office_id = request.office_id,
                        user_id = request.user_id,
                        IsDelete = false,
                    };
                    await _storageRepository.UpdateAsync(storage);
                    //给入库产品信息添加
                    foreach (var item in request.StorageProducts)
                    {
                        StorageProducts storageProducts = new StorageProducts()
                        {
                            StorageProductsId = item.StorageProductsId,
                            StorageId = storage.StorageId,
                            ProductNo = item.ProductNo,
                            ProductName = item.ProductName,
                            ProductSpec = item.ProductSpec,
                            Batch = item.Batch,
                            Price = item.Price,
                            StorageNum = item.StorageNum,
                            TotalPrice = item.TotalPrice,
                            BinLocationName = item.BinLocationName,
                            office_id = storage.office_id,
                            user_id = storage.user_id,
                            CreateTime = request.CreateTime,
                            UpdateTime = now,
                            IsDelete = false,
                        };
                        await _storageProductsRepository.UpdateAsync(storageProducts);
                    }
                    var list = await _storageRepository.DB.SaveChangesAsync();
                    tran.Commit();
                    result.Code = list;
                }
                catch (Exception el)
                {
                    result.Code = -1;
                    result.Message = el.Message;
                    _logger.LogError(el, el.Message);
                    tran.Rollback();
                }
            }
            return result;
        }
    }
}
